<template>
  <div class="content-wrapper">
    <div class="content-container">
      <el-table
        v-if="dataList.length > 0"
        v-loading="listLoading"
        height="65vh"
        :data="
          dataList.filter(
            (data) =>
              !search ||
              data.userName.toLowerCase().includes(search.toLowerCase())
          )
        "
        highlight-current-row
        default-expand-all
      >
        <!-- <el-table-column type="selection" width="55" /> -->
        <el-table-column label="实操人员" prop="userName" width="200px">
          <template slot="header" slot-scope="scope">
            <el-input
              v-model="search"
              size="mini"
              placeholder="根据实操人员搜索"
            />
          </template>
        </el-table-column>

        <el-table-column label="所属部门" prop="userGroupName" width="160px" />
        <el-table-column label="更新时间" prop="updateTime" min-width="200px" />
        <!-- <el-table-column label="答题时间" prop="handInTime" width="160px" />
        <el-table-column label="最高分" prop="score" width="100px" /> -->
        <el-table-column label="是否合格" prop="passFlag" width="100px">
          <template slot-scope="{ row }">
            <el-tag
              v-if="isPassFlag(row.practiceRecordResponses)===1"
              size="mini"
              type="success"
            >合格</el-tag>
            <el-tag
              v-if="isPassFlag(row.practiceRecordResponses)===2"
              size="mini"
              type="danger"
            >不合格</el-tag>
            <el-tag v-if="isPassFlag(row.practiceRecordResponses)===3" size="mini" type="info">未参加</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="150px"
          fixed="right"
        >
          <template slot-scope="{ row }">
            <el-dropdown v-if="row.trainId" trigger="click" class="filter-item">
              <el-button type="text" class="el-icon-more" />
              <el-dropdown-menu slot="dropdown" class="user-dropdown">
                <el-dropdown-item>
                  <el-popover placement="left" width="400" trigger="hover">
                    <el-table
                      class="my-popover-table"
                      :data="row.practiceRecordResponses"
                      @row-click="viewAttachment"
                    >
                      <el-table-column property="updateTime" label="上传日期" />
                      <el-table-column
                        width="100"
                        property="practiceDuration"
                        label="操作时长"
                      />
                      <el-table-column label="是否合格" prop="passFlag">
                        <template slot-scope="{ row }">
                          <el-tag
                            v-if="row.passFlag == 1"
                            size="mini"
                            type="success"
                          >合格</el-tag>
                          <el-tag
                            v-else-if="row.passFlag == 2"
                            size="mini"
                            type="danger"
                          >不合格</el-tag>
                          <el-tag v-else size="mini" type="info">未评</el-tag>
                        </template>
                      </el-table-column>
                    </el-table>
                    <span slot="reference"> 查看 </span>
                  </el-popover>
                </el-dropdown-item>
                <!-- <el-dropdown-item @click.native="viewAttachment(row)"
                  >查看</el-dropdown-item
                > -->
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      title="查看"
      :visible.sync="practiceDialog"
      class="mini"
      @close="closePracticeDialog"
    >
      <el-form>
        <el-row><el-col :span="22">
          <el-form-item label="实操时长">
            <el-input
              v-model="practiceDuration"
              :disabled="dialogStatus == 'update'"
              size="medium"
              type="number"
              style="margin-bottom: 20px; width: 50%"
              placeholder="请输入时间，单位秒"
            />
          </el-form-item> </el-col></el-row>
        <el-row><el-col :span="22">
          <el-form-item label="插入附件">
            <el-upload
              v-if="dialogStatus == 'create'"
              class="upload-demo"
              :action="uploadUrl"
              :on-preview="filePreview"
              :file-list="fileList"
              :data="paramsData"
              :headers="headers"
              :on-success="fileUploadSuccess"
              list-type="picture"
            >
              <el-button icon="el-icon-upload2" size="mini" circle />
            </el-upload>
          </el-form-item> </el-col></el-row>
      </el-form>

      <el-upload
        v-if="dialogStatus == 'update'"
        class="upload-demo upload_test"
        :action="uploadUrl"
        :on-preview="filePreview"
        :file-list="fileList"
        list-type="picture"
      />

      <el-radio
        v-if="dialogStatus == 'update'"
        v-model="saveData.passFlag"
        :disabled="data.passFlag != 0"
        style="margin-top: 10px"
        :label="1"
      >合格</el-radio>
      <el-radio
        v-if="dialogStatus == 'update'"
        v-model="saveData.passFlag"
        :disabled="data.passFlag != 0"
        :label="2"
      >不合格</el-radio>
      <el-input
        v-if="dialogStatus == 'update'"
        v-model="saveData.comment"
        :disabled="data.passFlag != 0"
        style="margin-top: 10px"
        type="textarea"
        max-length="200"
        placeholder="请输入评语"
      />

      <span slot="footer" class="dialog-footer">
        <el-button @click="practiceDialog = false">取 消</el-button>
        <el-button
          :disabled="data.passFlag != 0"
          type="primary"
          @click="
            dialogStatus == 'create' ? savePracticeRecord() : saveComment()
          "
        >保 存</el-button>
      </span>
    </el-dialog>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
import { get, update } from '@/api/ts/practice'

export default {
  props: ['examinationData'],
  data() {
    return {
      uploadUrl:
        process.env.VUE_APP_BASE_API + '/api/base/attachment/upload_attachment',
      dataList: [],
      total: 0,
      paperOptions: [],
      userGroupOptions: [],
      listQuery: {
        pageIndex: 0,
        pageNumber: 1,
        pageSize: 5,
        examineePaperId: null,
        examineeUserGroupId: null,
        examineeUserName: null,
        passFlag: null
      },
      listLoading: false,
      recordViewDialog: false, // 查看对话框
      propsData: null,
      practiceDialog: false,
      dialogStatus: '',
      practiceDuration: 0,
      data: {
        passFlag: 0
      },
      saveData: {
        practiceRecordId: null,
        comment: '',
        passFlag: 1
      },
      dialogImageUrl: '',
      search: '',
      dialogVisible: false
    }
  },
  watch: {
    examinationData(data) {
      this.dataList = data.data
    }
  },
  mounted() {
    if (this.examinationData) {
      console.log(this.examinationData, 'examinationData')
    }
  },
  methods: {
    viewRecord(row) {
      this.$emit(
        'viewRecord',
        row.examinationRecordResponses[0].examinationRecordId
      )
    },
    handleFilter() {},
    viewAttachment(row) {
      get({ practiceRecordId: row.practiceRecordId }).then((res) => {
        this.practiceDuration = res.data.practiceDuration
        this.saveData.practiceRecordId = res.data.practiceRecordId
        this.data = res.data
        this.saveData.comment = res.data.comment
        if (res.data.passFlag != 0) {
          this.saveData.passFlag = res.data.passFlag
        }
        this.fileList = []
        if (res.data.attachmentListResult) {
          res.data.attachmentListResult.forEach((item) => {
            var data = {
              name: item.fileName,
              url: item.objectUrl
            }
            this.fileList.push(data)
            this.practiceDialog = true
            this.dialogStatus = 'update'
          })
        }
      })
    },
    closePracticeDialog() {
      this.gid = null
      this.fileList = []
      this.practiceDuration = 0
    },
    filePreview(file) {
      // 文件详细查看
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    saveComment() {
      update(this.saveData).then((res) => {
        this.$message.success('保存成功')
        this.practiceDialog = false
        this.$emit('getDataList')
      })
    },
    isPassFlag(row) {
      let isFlag = 0
      if (row.length > 0) {
        row.forEach((e) => {
          if (e.passFlag === 1) {
            isFlag = 1
          } else if (e.passFlag === 2) {
            isFlag = 2
          }
        })
        return isFlag
      } else {
        return (isFlag = 3)
      }
    }
  }
}
</script>
<style scope>
.view_record .el-dialog .el-dialog__header {
  padding: 0px !important;
}
.view_record .el-dialog .el-dialog__body {
  padding: 30px 0px !important;
}
</style>
